package huiji;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.math.BigDecimal;
import java.net.URL;
import java.util.List;

//
public class BaseObj {
    List<HuijiImage> imageList;//对象使用多个图片切换进行展示
    int index;//表示图片的下标

    //元素图片
    BufferedImage image;
    //宽度
    int width;
    //高度
    int height;

    //x坐标
    int x;
    //y坐标
    int y;
    //运动速度
    int speed;

    public BaseObj() {
    }

    public BaseObj(URL imageuRL){
        this(gainImage(imageuRL));
    }

    public BaseObj(BufferedImage image) {
        this.image = image;
        this.width = image.getWidth();
        this.height = image.getHeight();
    }

    public static BufferedImage gainImage(URL imageuRL) {
        try{
            return ImageIO.read(imageuRL);
        }catch (Exception e){
            return null;
        }

    }



    public BufferedImage getImage() {
        return image;
    }

    public BaseObj setImage(BufferedImage image) {
        this.image = image;
        return this;
    }
    public BaseObj setImageAndWH(BufferedImage image) {
        this.image = image;
        this.width = this.image.getWidth();
        this.height = this.image.getHeight();
        return this;
    }

    public int getWidth() {
        return width;
    }

    public BaseObj setWidth(int width) {
        this.width = width;
        return this;
    }

    public int getHeight() {
        return height;
    }

    public BaseObj setHeight(int height) {
        this.height = height;
        return this;
    }

    public int getX() {
        return x;
    }

    public BaseObj setX(int x) {
        this.x = x;
        return this;
    }

    public int getY() {
        return y;
    }

    public BaseObj setY(int y) {
        this.y = y;
        return this;
    }

    public int getSpeed() {
        return speed;
    }

    public BaseObj setSpeed(int speed) {
        this.speed = speed;
        return this;
    }

    public List<HuijiImage> getImageList() {
        return imageList;
    }

    public BaseObj setImageList(List<HuijiImage> imageList) {
        this.imageList = imageList;
        return this;
    }

    public int getIndex() {
        return index;
    }

    public BaseObj setIndex(int index) {
        this.index = index;
        return this;
    }

    public void paint(Graphics g){
        g.drawImage(image,x,y,null);
    }

    public void paintOneOfList(Graphics g){
        g.drawImage(this.imageList.get(index).getImage(),x,y,null);
    }



    /**
     * 移动
     */
    public void move(){

    }

    /**
     * 焦点碰撞判断，入参坐标在对象坐标范围内返回true
     */
    public boolean touch(int mouseX,int mouseY){
        //System.out.println("touch_x:"+mouseX+";this.x:"+this.x+"this.width:"+this.width+";y:"+mouseY+";this.y:"+this.y+"this.height:"+this.height);
        return mouseX>=this.x&&mouseX<=this.x+this.width&&mouseY>=this.y&&mouseY<=this.y+this.height;

    }

    /**
     * 对象碰撞判断，
     */
    public boolean touch(BaseObj baseObj){
        //获取对象中点坐标
        int x1 = this.x+this.width/2;
        int y1 = this.y+this.height/2;
        int x2 = baseObj.getX()+baseObj.getWidth()/2;
        int y2 = baseObj.getY()+baseObj.getHeight()/2;
        if(new BigDecimal(String.valueOf(x1-x2)).abs().compareTo(new BigDecimal(String.valueOf(this.width/2+baseObj.getWidth()/2)))!=1
            &&new BigDecimal(String.valueOf(y1-y2)).abs().compareTo(new BigDecimal(String.valueOf(this.height/2+baseObj.getHeight()/2)))!=1){
            //两对象碰撞
            return true;
        }else{
            return false;
        }


    }

    /**
     * 是否越界判断
     */
    public boolean isOut(){
        return this.x<0-this.width||this.x>HuijiJPanel.width||this.y<0-this.height||this.y>HuijiJPanel.height;

    }
}
